package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IWeChatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;

/**
 * 老人家属Service业务层处理
 *
 * @author syl
 * @date 2025-10-16
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper,FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    /**
     * 查询老人家属
     *
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     *
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember)
    {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember)
    {
        return save(familyMember)?1:0;
    }

    /**
     * 修改老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember)
    {
        return updateById(familyMember)?1:0;
    }

    /**
     * 批量删除老人家属
     *
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除老人家属信息
     *
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id)
    {
        return removeById(id)?1:0;
    }



    @Autowired
    private IWeChatService weChatService;

    @Autowired
    private TokenService tokenService;

    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        // 1、调用微信的接口 返回一个openid
        String openid =  weChatService.getOpenid(dto.getCode());

        // 2、根据openid查询家属
        LambdaQueryWrapper<FamilyMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FamilyMember::getOpenId,openid);
        FamilyMember familyMember = this.getOne(queryWrapper);

        // 3、调用微信的接口获取用户的手机号
        String phone = weChatService.getPhone(dto.getPhoneCode());

        // 4、如果查不到 直接新增
        if(familyMember==null){
            familyMember = new FamilyMember();

            familyMember.setPhone(phone);
            String nickName = DEFAULT_NICKNAME_PREFIX.get(new Random().nextInt(DEFAULT_NICKNAME_PREFIX.size()+1)) + phone.substring(7);
            familyMember.setName(nickName);  // 昵称 从DEFAULT_NICKNAME_PREFIX集合中随机挑选一个+手机号的后4位
            familyMember.setOpenId(openid);
            this.save(familyMember);  //MP
        }else{
            // 5、判断手机号是否有变化，如果有变化更新家属信息
            if(!familyMember.getPhone().equals(phone)){
                familyMember.setPhone(phone);
                this.updateById(familyMember);
            }
        }

        // 6、生成token  TokenService
        Map<String, Object> map = Map.of("id", familyMember.getId());
        String token = tokenService.createToken(map);//注意 要把private 改成public


        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(familyMember.getName());

        return loginVo;
    }


    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of("生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );
}
